package com.lhl.netty.server;

import com.lhl.netty.common.NetMessage;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/29.
 */
public class MessageParserServer {
	private final static Logger logger = LoggerFactory.getLogger(MessageParserServer.class);

	private static Map<String,Channel> channels = new HashMap<String, Channel>();


    
    public static void addClient(String id,Channel channel){
    	channels.put(id, channel);
    }
    
    public static void removeClient(String id){
    	channels.remove(id);
    }
    
    public static boolean isClientOnline(String id){
    	Channel channel = channels.get(id);
    	return channel!=null&&channel.isActive();
    }
    
    public static void send2All(NetMessage msg){
		Channel channel = null;
		for (String key : MessageParserServer.channels.keySet()) {
			channel = MessageParserServer.channels.get(key);
			if(channel!=null&&channel.isActive()){
				channel.writeAndFlush(msg);
			}
        }
	}
	
	public static void send2Client(String id,NetMessage msg){
		Channel channel = MessageParserServer.channels.get(id);
		if(channel!=null){
			channel.writeAndFlush(msg);
		}
	}
	

	
}
